<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AdminModels\Admin;
use Log;
class AdminAjaxController extends Controller{
    private $code = false;
    private $parames;
    private $message = '';
    private $data = '';
    
    public function __construct(Request $request){
        $this->parames = $request->all();
    }
    
    /**
     * 登录验证
     */
    public function login(){
        if(isset($this->parames['username']) && !empty($this->parames['username']) && isset($this->parames['password']) && !empty($this->parames['password'])){
			if(!isset($this->parames['yzmcode']) || empty($this->parames['yzmcode'])){
				$this->code = true;
				$this->message = '验证码不能为空';
			}else{
				$yzmcode=session('yzmcode');
				if($yzmcode!==strtolower($this->parames['yzmcode'])){
					$this->code = true;
					$this->message = '验证码错误';
				}else{
					$oAdmin = new Admin;
					$result = $oAdmin->validAdminLogin($this->parames['username'], $this->parames['password']);
					if(!empty($result)){
						session()->forget('user');
						session()->forget('userLimits');
						$userinfo = array(
							'admin_id'=>$result['admin_id'],
							'admin_name'=>$result['admin_name'],
							'real_name'=>$result['real_name'],
							'head_img'=>$result['head_img'],
							'power'=>$result['power'],
							'role_id'=>$result['role_id'],
						);
						$userLimits = $oAdmin->getUserLimitInfo($result['role_id'],'limit_url');
						session()->push('user', $userinfo);
						session()->push('userLimits', $userLimits);
					}else{
						$this->code = true;
						$this->message = '用户名或密码错误';
					}
				}
			}
        }else{
            $this->code = true;
            $this->message = '用户名密码不能为空';
        }
        echo json_encode(['code'=>$this->code,'message'=>$this->message,'data'=>$this->data]);
    }
    
    /**
     * 修改密码验证
     */

    public function updatePassword(Request $request){

        if(!$request->session()->has('user')){
            $this->code = true;
            $this->message = '登录已过时，请重新登录';
        }else{
            if(isset($this->parames['password']) && !empty($this->parames['password'])){
                $oAdmin = new Admin;
                $result = $oAdmin->validAdminLogin($request->session()->get('user')[0]['admin_name'], $this->parames['password']);
                if(!$result){
                    $this->code = 1;
                    $this->message = '原密码错误';
                }else{
                    $checkUpwd=\App\Components\Tools::checkUpwd($this->parames['new_password']);
                    if(!$checkUpwd){
                        $this->code = 1;
                        $this->message = '请输入6-16位 字母、数字、下划线，任意字符的密码';
                    }else{
                        $id=$request->session()->get('user')[0]['admin_id'];
                        $pwd=\App\Components\Tools::passwordEncryption($this->parames['new_password']);
                        $data=['password'=>$pwd];
                        $re=$oAdmin->db_update('',$id,$data);
                        if($re){
                            session()->forget('user');
                            $this->code = 0;
                            $this->message = '修改成功';
                        }else{
                            $this->code = 1;
                            $this->message = '修改失败';
                        }
                    }
                }
            }
        echo json_encode(['code'=>$this->code,'message'=>$this->message,'data'=>$this->data]);
    }
    }
    
    /**
     * 管理员列表
     * ajax数据
     */
    public function adminList(){
        $oAdmin = new Admin;
        $result = $oAdmin->getPageData($this->parames);
        echo json_encode($result);
    }
    
    /**
     * 验证账号是否存在
     */
    public function validAdminName(){
        if(isset($this->parames['username']) && !empty($this->parames['username']) && isset($this->parames['mobile'])){
          $oAdmin = new Admin;
          $admin_id = isset($this->parames['admin_id']) && $this->parames['admin_id']>0 ? $this->parames['admin_id'] :0;
          $result = $oAdmin->getAdminData($this->parames['username'],$this->parames['mobile']);
            if(!empty($result)){
              if(count($result) > 1){
                $this->code = true;
                $this->message = '账号或者手机号已经存在';
              }elseif($result[0]['admin_id'] != $admin_id){
                $this->code = true;
                $this->message = '账号或者手机号已经存在';
              }
            }else{
                $this->data = true;
            }
        }else{
            $this->code = true;
            $this->message = '缺少数据';
        }
      echo json_encode(['code'=>$this->code,'message'=>$this->message,'data'=>$this->data]);
    }
    
    /**
     * 操作用户状态
     * 开启/关闭
     */
    public function actionAdmin(Request $request){
        if(!$request->session()->has('user')){
            $this->code = true;
            $this->message = '登录已过时，请重新登录';
        }else{
            $url = $_SERVER['REQUEST_URI'];
            $result = \App\Components\Tools::urlLimit($url,$request);
            if(!$result){
                $this->code = true;
                $this->message = '无权限操作';
            }else{
                if(isset($this->parames['id']) && intval($this->parames['id']) > 0 && isset($this->parames['status'])){
                    $oAdmin = new Admin;
                    $result = $oAdmin->getData($this->parames['id']);
                    if(!empty($result)){
                        if($oAdmin->db_update('',$this->parames['id'],['status'=>$this->parames['status']])){
                            $this->data = true;
                        }else{
                            $this->code = true;
                            $this->message = '操作失败';
                        }
                    }else{
                        $this->code = true;
                        $this->message = '账号不存在';
                    }
                }else{
                    $this->code = true;
                    $this->message = '缺少数据';
                }
            }
        }
        echo json_encode(['code'=>$this->code,'message'=>$this->message,'data'=>$this->data]);
    }
}